package com.todo.biz.application.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.biz.common.enums.DetectionRiskLevelEnum;
import com.todo.biz.application.convert.DetectionRiskLevelConvert;
import com.todo.biz.application.entity.bo.BizImgExamineBO;
import com.todo.biz.application.entity.bo.BizTextExamineBO;
import com.todo.biz.application.entity.bo.ContentExamineData;
import com.todo.biz.application.entity.context.ContentReviewContext;
import com.todo.biz.application.entity.context.ContentReviewResult;
import com.todo.biz.application.handler.review.content.AbstractContentReview;
import com.todo.biz.application.handler.review.content.ReviewHandleFactory;
import com.todo.biz.application.helper.detection.AliyunDetectionHelper;
import com.todo.biz.application.helper.detection.entity.ImgDetectionResponse;
import com.todo.biz.application.helper.detection.entity.TextDetectionResponse;
import com.todo.biz.application.service.BizExamineRecordService;
import com.todo.biz.application.entity.enums.ContentReviewEnum;
import com.todo.biz.common.enums.DetectionType;
import com.todo.biz.common.enums.ReviewLevelEnum;
import com.todo.biz.infrastructure.entity.po.DetectionRecordPO;
import com.todo.biz.infrastructure.repository.DetectionRecordRepository;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (BizExamineRecord)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:44
 */
@Slf4j
@Service("bizExamineRecordService")
public class BizExamineRecordServiceImpl implements BizExamineRecordService {

    @Resource
    private ReviewHandleFactory reviewHandleFactory;
    @Resource
    private AliyunDetectionHelper aliyunDetectionHelper;
    @Resource
    private DetectionRecordRepository detectionRecordRepository;

    @Override
    public String localSensitiveWordsDetection(String content, BusinessSceneEnum bizEnum) {
        if (StringUtils.isBlank(content)) {
            return "";
        }
        AbstractContentReview contentReview = reviewHandleFactory.getContentReview(ContentReviewEnum.SENSITIVE_WORDS);
        if (contentReview == null) {
            log.error("BizExamineRecordService#baseSensitiveWordsExamine 基础敏感词过滤失败，数据调用为空 content:{}", content);
            return null;
        }
        ContentReviewContext context = new ContentReviewContext();
        context.setContent(content);
        context.setSceneEnum(bizEnum);
        ContentReviewResult contentReviewResult = contentReview.contentReviewHandle(context);
        if (contentReviewResult == null || !contentReviewResult.getReviewResult()) {
            log.error("BizExamineRecordService#baseSensitiveWordsExamine 基础敏感词过滤失败，数据返回异常 content:{}", content);
            return null;
        }

        if (ReviewLevelEnum.A.equals(contentReviewResult.getReviewLevelEnum())) {
            return content;
            //B、C返回调整后的数据
        } else if (ReviewLevelEnum.B.equals(contentReviewResult.getReviewLevelEnum()) || ReviewLevelEnum.C.equals(contentReviewResult.getReviewLevelEnum())) {
            return contentReviewResult.getFailedDetail().getResultContext();
        } else {
            //其余状态统一不返回结果数据
            return "";
        }
    }

    @Override
    public ContentExamineData contentDetection(BizTextExamineBO bizTextExamineBO) {
        if (bizTextExamineBO == null || bizTextExamineBO.getBizId() == null) {
            return null;
        }
        TextDetectionResponse textDetectionResponse = aliyunDetectionHelper.generalTextDetection(bizTextExamineBO.getContent(), bizTextExamineBO.getBizId(), bizTextExamineBO.getSceneEnum());
        if (textDetectionResponse == null) {
            log.error("BizExamineRecordService#contentDetection 检测文本内容异常，入参:{}", JSONObject.toJSONString(bizTextExamineBO));
            return null;
        }

        //存储检测数据
        DetectionRecordPO detectionRecordPO = new DetectionRecordPO();
        detectionRecordPO.setBizId(bizTextExamineBO.getBizId());
        detectionRecordPO.setBusinessScene(bizTextExamineBO.getSceneEnum().getScene());
        detectionRecordPO.setDetectionCode(DetectionType.TEXT_BASE_LINE.getCode());
        detectionRecordPO.setInfo(bizTextExamineBO.getContent());
        detectionRecordPO.setDetectionResult(JSONObject.toJSONString(textDetectionResponse));
        Boolean inserted = detectionRecordRepository.insertDetectionRecord(detectionRecordPO);
        if (inserted) {
            log.error("BizExamineRecordService#contentDetection 检测文本内容异常，入参:{}", JSONObject.toJSONString(bizTextExamineBO));
        }
        ContentExamineData contentExamineData = new ContentExamineData();
        contentExamineData.setBizId(bizTextExamineBO.getBizId());
        contentExamineData.setSceneEnum(bizTextExamineBO.getSceneEnum());
        if (textDetectionResponse.getReason() != null) {
            contentExamineData.setRiskTips(textDetectionResponse.getReason().getRiskTips());
            contentExamineData.setReviewLevelEnum(DetectionRiskLevelConvert.valueOfDetectionRiskLevelEnum(textDetectionResponse.getReason().getRiskLevel()));
        } else {
            contentExamineData.setRiskTips(textDetectionResponse.getLabels().getDesc());
            contentExamineData.setReviewLevelEnum(DetectionRiskLevelEnum.NONE);
        }
        return contentExamineData;
    }

    @Override
    public ContentExamineData commentDetection(BizTextExamineBO bizTextExamineBO) {
        if (bizTextExamineBO == null || bizTextExamineBO.getBizId() == null) {
            return null;
        }
        TextDetectionResponse textDetectionResponse = aliyunDetectionHelper.generalTextDetection(bizTextExamineBO.getContent(), bizTextExamineBO.getBizId(), bizTextExamineBO.getSceneEnum());
        if (textDetectionResponse == null) {
            log.error("BizExamineRecordService#commentDetection 检测评论内容异常，入参:{}", JSONObject.toJSONString(bizTextExamineBO));
            return null;
        }

        //存储检测数据
        DetectionRecordPO detectionRecordPO = new DetectionRecordPO();
        detectionRecordPO.setBizId(bizTextExamineBO.getBizId());
        detectionRecordPO.setBusinessScene(bizTextExamineBO.getSceneEnum().getScene());
        detectionRecordPO.setDetectionCode(DetectionType.COMMENT.getCode());
        detectionRecordPO.setInfo(bizTextExamineBO.getContent());
        detectionRecordPO.setDetectionResult(JSONObject.toJSONString(textDetectionResponse));
        Boolean inserted = detectionRecordRepository.insertDetectionRecord(detectionRecordPO);
        if (inserted) {
            log.error("BizExamineRecordService#commentDetection 检测评论内容异常，入参:{}", JSONObject.toJSONString(bizTextExamineBO));
        }
        ContentExamineData contentExamineData = new ContentExamineData();
        contentExamineData.setBizId(bizTextExamineBO.getBizId());
        contentExamineData.setSceneEnum(bizTextExamineBO.getSceneEnum());
        if (textDetectionResponse.getReason() != null) {
            contentExamineData.setRiskTips(textDetectionResponse.getReason().getRiskTips());
            contentExamineData.setReviewLevelEnum(DetectionRiskLevelConvert.valueOfDetectionRiskLevelEnum(textDetectionResponse.getReason().getRiskLevel()));
        } else {
            contentExamineData.setRiskTips(textDetectionResponse.getLabels().getDesc());
            contentExamineData.setReviewLevelEnum(DetectionRiskLevelEnum.NONE);
        }
        return contentExamineData;
    }

    @Override
    public ContentExamineData imgDetection(BizImgExamineBO bizImgExamineBO) {
        if (bizImgExamineBO == null || bizImgExamineBO.getBizId() == null) {
            return null;
        }
        ImgDetectionResponse imgDetectionResponse = aliyunDetectionHelper.baseLineImgDetection(bizImgExamineBO.getImgUrl(), bizImgExamineBO.getBizId(), bizImgExamineBO.getSceneEnum());
        if (imgDetectionResponse == null) {
            log.error("BizExamineRecordService#commentDetection 检测评论内容异常，入参:{}", JSONObject.toJSONString(bizImgExamineBO));
            return null;
        }

        //存储检测数据
        DetectionRecordPO detectionRecordPO = new DetectionRecordPO();
        detectionRecordPO.setBizId(bizImgExamineBO.getBizId());
        detectionRecordPO.setBusinessScene(bizImgExamineBO.getSceneEnum().getScene());
        detectionRecordPO.setDetectionCode(DetectionType.IMG_BASE_LINE.getCode());
        detectionRecordPO.setInfo(bizImgExamineBO.getImgUrl());
        detectionRecordPO.setDetectionResult(JSONObject.toJSONString(imgDetectionResponse));
        Boolean inserted = detectionRecordRepository.insertDetectionRecord(detectionRecordPO);
        if (inserted) {
            log.error("BizExamineRecordService#commentDetection 检测评论内容异常，入参:{}", JSONObject.toJSONString(bizImgExamineBO));
        }
        ContentExamineData contentExamineData = new ContentExamineData();
        contentExamineData.setBizId(bizImgExamineBO.getBizId());
        contentExamineData.setSceneEnum(bizImgExamineBO.getSceneEnum());
        contentExamineData.setRiskTips(JSONObject.toJSONString(imgDetectionResponse.getResults()));
        contentExamineData.setReviewLevelEnum(DetectionRiskLevelConvert.valueOfDetectionRiskLevelEnum(imgDetectionResponse.getRiskLevel()));
        return contentExamineData;
    }
}
